package com.netty.client;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

@Slf4j(topic="com.netty.client.RunnableHandler")
public class RunnableHandler implements Runnable{
    String msgString;
    WebSocketClientHandler wc;

    public RunnableHandler(WebSocketClientHandler wc,String msgString){
        this.msgString=msgString;
        this.wc=wc;
    }

    @Override
    public void run() {
        if (WebSocketClientHandler.isPrintMsg) {
            log.info("接收消息{}", msgString);
        }

        JSONObject msgJson=null,__heads=null;
        String __type=null,__code=null,number=null;
        try {
            msgJson = JSONObject.parseObject(msgString);
            __type = msgJson.getString("__type");
            __code = msgJson.getString("__code");
            number = msgJson.getString("number");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("RunnableHandler.run 1 接收到的消息{}",msgString);
            log.error("RunnableHandler.run 1 错误信息 e.getMessage{}",e.getMessage());
            //onMessage(null,msgString);
        }
        try {
            __heads= msgJson.getJSONObject("__heads");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("RunnableHandler.run 2 接收到的消息{}",msgString);
            log.error("RunnableHandler.run 2 错误信息2 __heads e.getMessage{}",e.getMessage());
            //onMessage(null,msgString);
        }

        try {
            if (__type!=null && __type.equals("success")) {
                wc.sockId = msgJson.getString("sockId");
                wc.onSuccess(msgJson);
            }else if (__type!=null && (__type.equals("online") || __type.equals("offline"))) {
                wc.sockId = msgJson.getString("sockId");
                wc.onNotice(msgJson);
            }else if (__type!=null && __type.equals("confirm")){
                //生产者接收 消费者 返回的消费确认信息
                wc.onMessageByConfirm(msgJson,msgString);
            }else if (__type!=null && __type.equals("error")){
                //返回服务器发送的错误消息
                wc.onMessageByError(msgJson,msgString,number);
            }else {
                boolean ret2=__code!=null && __code.length()>0;// && (queueType==QueueType.C_ONE || queueType==QueueType.C_MORE);
                JSONObject msgConfirm=null;
                if (ret2){
                    msgConfirm=new JSONObject(true);
                    //消费者 返回消费确认信息
                    msgConfirm.put("__type","confirm");
                    msgConfirm.put("number","S100");
                    msgConfirm.put("__code",__code);
                    if(__heads!=null) {
    //                        msgConfirm.put("pSockId", __heads.getString("pSockId"));//producerId=消息的发送者Id
    //                        msgConfirm.put("pUserId", __heads.getString("pSockId"));//producerId=消息的发送者Id
                        msgConfirm.put("__heads", __heads);//消息生产者的头信息
                    }
                }
                boolean ret=wc.onMessage(msgJson, msgString,msgConfirm);//先执行方法消费
                if (ret && ret2 && msgConfirm!=null){
                    wc.sendMessage(msgConfirm);//发送确认消息
                }
                if(msgConfirm!=null) msgConfirm.clear();msgConfirm=null;
            }
            if(msgJson!=null)msgJson.clear();msgJson=null;msgString=null;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("RunnableHandler.run 3 接收到的消息{}",msgString);
            log.error("RunnableHandler.run 3 错误信息2 __heads e.getMessage{}",e.getMessage());
            //onMessage(null,msgString);
        }
    }
}
